/**
 * Created by admin on 2017/10/26.
 */
$(document).ready(function() {
    var VAL = {
        NORMAL : "0000000",
        EVT_USER     : $("#userId").text(),
        T_FBPBISDAT  : "FBPBISDAT",
        T_MIPWOBOM   : "MIPWOBOM",
        T_MWPMTROPE  : "MWPMTROPE",
        T_FDPAPLYWO  : "FDPAPLYWO",
        DISABLED_ATTR : {
            'disabled' : true
        },
        ENABLED_ATTR : {
            'disabled' : false
        }

    };
    var domObj = {
        buttons : {
        	$query_btn : $("#query_btn"),
            $register_btn 	: $("#register_btn"),
            $clean_btn    	: $("#clean_btn"),
        },
        $WhIdSel    	 : $("#WhIdSel"),
        $actionIdSel    : $("#actionIdSel"),
        $operationIdSel  : $("#operationIdSel"),
        $woIdSel    	 : $("#woIdSel"),
        $bayIdSel       : $("#bayIdSel"),
        grids : {
            $mtrlListDiv 	: $("#mtrlListDiv"),
            $mtrlListGrd 	: $("#mtrlListGrd"),
            $mtrlListPg 	: $("#mtrlListPg")
        }
    };

    var actionType=[{
        data_ext :'261',
        data_desc:'261:工单领料',
    },{
        data_ext :'262',
        data_desc:'262:工单退料',
    },{
        data_ext :'Z15',
        data_desc:'Z15:报废仓领料',
    },{
        data_ext :'Z14',
        data_desc:'Z14:退报废仓',
    }];
    var iniGridInfo = function(){
        var mrtlListCM = [
            {name: 'mtrl_cate', index: 'mtrl_cate', label: MTRL_CATE_TAG, width: 80},
            {name: 'mtrl_prod_id_fk', index: 'mtrl_prod_id_fk', label: MTRL_ID_TAG, width: 200},
            {name: 'mtrl_prod_dsc', index: 'mtrl_prod_dsc', label: MTRL_PROD_DSC_TAG, width: 200},
            {name: 'mtrl_wh_qty', index: 'mtrl_wh_qty', label: MTRL_WH_QTY, width: 180},
            {name: 'mtrl_lot_id', index: 'mtrl_lot_id', label: MTRL_LOT_ID_TAG, editable:true, width: 200},
            {name: 'mtrl_qty', index: 'mtrl_qty', label: MTRL_QTY_TAG, editable:true, width: 180},
            {name: 'mtrl_unit', index: 'mtrl_unit', label: MTRL_UNIT_TAG, width: 100}
        ];
        var lastrow,lastcell;
        domObj.grids.$mtrlListGrd.jqGrid({
            url: "",
            datatype: "local",
            mtype: "POST",
            autowidth: true,
            shrinkToFit: false,
            scroll : true,
            resizable: true,
            loadonce: true,
            viewrecords: true,
            rownumbers: true,
            multiselect : true,
            emptyrecords: true,
            colModel: mrtlListCM,
            cellEdit:true,
            cellsubmit:"clientArray",
            pager: "#mtrlListPg"
        });
    };
    
    var toolFunc = {
	    //表格自适应
    	resizeFnc : function(){
	        var offsetBottom, divWidth;
	        divWidth = $("#mtrlListDiv").width();
	        offsetBottom =  $(window).height() - $("#mtrlListDiv").offset().top;
	        $("#mtrlListDiv").height(offsetBottom * 0.95);
	        $("#mtrlListGrd").setGridWidth(divWidth * 0.98);
	        $("#mtrlListGrd").setGridHeight(offsetBottom * 0.99 - 101);

	    },
        iniWHIdSel :function(){
            var inObj = {
                trx_id : VAL.T_FBPBISDAT,
                action_flg : "Q",
                iary : [{
                    data_cate: 'DEST',
                    data_item  : 'L'
                }]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                SelectDom.addSelectArr(domObj.$WhIdSel, outObj.oary, "data_ext","data_desc","",true);
            }
        },
        iniActionSel :function(){
            SelectDom.addSelectArr(domObj.$actionIdSel, actionType, "data_ext","data_desc","",true);
        },
        iniOperationSel :function(){
            var actionIdSel = domObj.$actionIdSel.val();
            if(actionIdSel==""){
               return false;
            }
            var inObj = {
                trx_id : VAL.T_FBPBISDAT,
                action_flg : "Q",
                iary : [{
                    data_cate: 'OPRA',
                    data_item  : actionIdSel
                }]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                SelectDom.addSelectArr(domObj.$operationIdSel, outObj.oary, "data_ext","data_desc","",true);
            }
        },
    	iniBayInfo : function(){
            comAddValueByDataCateFnc("#bayIdSel", "AREA", "data_ext", "data_desc", true);
    	},
        iniWoInfo : function(){//初始化工单
            var inObj = {
                trx_id: VAL.T_FDPAPLYWO,
                action_flg: "Q",
                iary: []
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                SelectDom.addSelectArr(domObj.$woIdSel, outObj.oary, "wo_id", "wo_id", "", true);
            }
        }
    };
    var btnFunc = {
        queryFunc:function(){
            var woIdFk = $.trim(domObj.$woIdSel.val());
            if(!woIdFk){
                showErrorDialog("","请选择工单代码！");
                return false;
            }
            var whIdSel = $.trim(domObj.$WhIdSel.val());
            if(!whIdSel){
                showErrorDialog("", "仓库代码不能为空");
                return false;
            }
            var inObj = {
                trx_id : VAL.T_MIPWOBOM,
                action_flg : "Q",
                wo_id_fk:woIdFk,
                dest_shop :whIdSel,
                iary : []
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                setGridInfo(outObj.oaryA, "#mtrlListGrd");
            }
        },
        registerFunc:function(){
            var whIdSel = $.trim(domObj.$WhIdSel.val());
            var actionIdSel = $.trim(domObj.$actionIdSel.val());
            var iniOperationSel = $.trim(domObj.$operationIdSel.val());
            var woIdFk = $.trim(domObj.$woIdSel.val());
            var bayId = $.trim(domObj.$bayIdSel.val());

            if(!whIdSel){
                showErrorDialog("", "仓库代码不能为空");
                return false;
            }
            if(!actionIdSel){
                showErrorDialog("", "操作类型不能为空");
                return false;
            }
            if(!iniOperationSel){
                showErrorDialog("", "业务类型不能为空");
                return false;
            }
            if(!bayId){
                showErrorDialog("", "线别代码不能为空");
                return false;
            }
            if(!woIdFk){
                showErrorDialog("", "工单代码不能为空");
                return false;
            }
            var ids = domObj.grids.$mtrlListGrd.jqGrid('getGridParam','selarrrow');
            if(ids.length<1){
                showErrorDialog("", "至少选择一条数据进行登记");
                return false;
            }
            var iary =[];
            for(var i=0;i<ids.length;i++){
                if ($('#mtrlListGrd :text[role="textbox"]').length > 0) {
                    var v = $('#mtrlListGrd :text[role="textbox"]').val();
                    $('#mtrlListGrd :text[role="textbox"]').parent().attr('title', v);
                    $('#mtrlListGrd :text[role="textbox"]').parent().html(v);
                }
                var rowData = domObj.grids.$mtrlListGrd.jqGrid("getRowData",ids[i]);
//                if(!rowData.mtrl_lot_id){
//                    showErrorDialog("", "第[" + $('#' + ids[i])[0].rowIndex + "]行物料批次号不能为空");
//                    return false;
//                }
                if(!rowData.mtrl_prod_id_fk){
                	showErrorDialog("", "第[" + $('#' + ids[i])[0].rowIndex + "]行物料代码不能为空");
                	return false;
                }
                if(!rowData.mtrl_qty){
                    showErrorDialog("", "第[" + $('#' + ids[i])[0].rowIndex +"]行物料数量不能为空");
                    return false;
                }else if(!ComRegex.isNumber(rowData.mtrl_qty)){
                	showErrorDialog("", "第" + $('#' + ids[i])[0].rowIndex + "行物料数量数值格式不正确！");
                    return false;
                }
                if(actionIdSel=='261'||actionIdSel=='Z15'){
                    if(parseInt(rowData.mtrl_qty)>parseInt(rowData.mtrl_wh_qty)){
                        showErrorDialog("", "第[" + $('#' + ids[i])[0].rowIndex + "]行,领料操作时物料数量不能大于库存数量");
                        return false;
                    }
                }
                iary.push(rowData);
            }
            var inObj = {
                trx_id      :  VAL.T_MWPMTROPE,
                action_flg  :  "A",
                evt_usr     :  VAL.EVT_USER,
                wh_id_fk    :  whIdSel,
                action_type :  actionIdSel,
                operation_type :iniOperationSel,
                wo_id_fk    :  woIdFk,
                line_id     :  bayId,
                iary : iary
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
               if(actionIdSel=='261'||actionIdSel=='Z15'){
                   showSuccessDialog("工单领料成功");
               }else{
                   showSuccessDialog("工单退料成功");
               }
                btnFunc.queryFunc();
            }

        },
        clearFunc : function(){
            domObj.grids.$mtrlListGrd.jqGrid("clearGridData")
            SelectDom.setSelect(domObj.$WhIdSel, "", "");
            SelectDom.setSelect(domObj.$woIdSel, "", "");
        },
    };
    /**
     * Ini contorl's data
     */
    var iniContorlData = function(){
        toolFunc.iniActionSel();
        toolFunc.iniOperationSel();
        toolFunc.iniWHIdSel();
        toolFunc.iniBayInfo();
        toolFunc.iniWoInfo();

    };

    /**
     * Bind button click action
     */
    var iniButtonAction = function(){
        domObj.buttons.$query_btn.click(function(){
            btnFunc.queryFunc();
        });
        domObj.buttons.$register_btn.click(function(){
            btnFunc.registerFunc();
        });
        domObj.buttons.$clean_btn.click(function(){
            btnFunc.clearFunc();
        });
        domObj.$actionIdSel.change(function(){
            toolFunc.iniOperationSel();
        });
    };


    /**
     * Other action bind
     */
    var otherActionBind = function(){
        $("form").submit(function(){
            return false;
        });
        $(window).resize(function() {
        	toolFunc.resizeFnc();
        });
        
        domObj.$woIdSel.change(function(){
        	domObj.grids.$mtrlListGrd.jqGrid("clearGridData");
        });
    };
    /**
     * Ini view, data and action bind
     */
    var initializationFunc = function(){
        iniGridInfo();
        toolFunc.resizeFnc();
        iniContorlData();
        iniButtonAction();
        otherActionBind();
    };

    initializationFunc();
});
